/**
 * 
 */
package kr.co.insoft.core.service.configration.sev.impl;

import java.util.HashMap;
import java.util.List;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.model.configration.sev.SEVEntityModel;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.repository.configration.sev.SEVMetadataRepository;
import kr.co.insoft.core.repository.configration.sev.SEVRepository;
import kr.co.insoft.core.service.configration.sev.SEVService;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import flexjson.JSONDeserializer;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Service
public class SEVServiceImpl implements SEVService {
	@Autowired
	SEVRepository sevRepository;

	private static final Logger SEVServiceImplLog = LoggerFactory
			.getLogger(SEVServiceImpl.class);

	@Override
	public PagingModel getLists(PagingModel pagingModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			pagingModel.setSearchOrCondition(user.getQueryAuthParam());
			List<SEVEntityModel> lists = sevRepository.getLists(pagingModel);
			int totalCount = sevRepository.getTotalCount(pagingModel);
			return new PagingModel(lists, totalCount, pagingModel);
		} catch (RepositoryException e) {
			SEVServiceImplLog.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Autowired
	SEVMetadataRepository sevMetadataRepository;

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void createSEV(SEVEntityModel sevEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			sevEntityModel.setAuth((User) auths.getPrincipal());
			/* Variable 등록 */
			sevEntityModel = sevRepository.createSEV(sevEntityModel);
			/* Variable Value 등록 */
			sevMetadataRepository.createSEVMetadata(sevEntityModel);
		} catch (Exception e) {
			SEVServiceImplLog.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	private SEVEntityModel parseJsonData(String sevEntityData)
			throws ServiceException {
		try {
			JSONDeserializer<HashMap<String, Object>> deserializer = new JSONDeserializer<HashMap<String, Object>>();
			HashMap<String, Object> deserializeResult = deserializer
					.deserialize(sevEntityData);
			SEVEntityModel sevEntityModel = new SEVEntityModel();
			BeanUtils.populate(sevEntityModel, deserializeResult);
			return sevEntityModel;
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void createSEV(String sevEntityData) throws ServiceException {
		createSEV(parseJsonData(sevEntityData));
	}

	@Override
	public SEVEntityModel getSEV(String variableid) throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();

			SEVEntityModel sevEntityModel = new SEVEntityModel();
			sevEntityModel.setVariableid(variableid);
			sevEntityModel.setAuth((User) auths.getPrincipal());
			sevEntityModel = sevRepository.getSEV(sevEntityModel);
			SEVCompute compute = new SEVCompute(
					sevMetadataRepository.getSEVMetadata(sevEntityModel));
			sevEntityModel.setVariables(compute.getVariablesAsTable());
			return sevEntityModel;
		} catch (RepositoryException e) {
			SEVServiceImplLog.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void editSEV(SEVEntityModel sevEntityModel) throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			sevEntityModel.setAuth((User) auths.getPrincipal());

			sevRepository.editSEV(sevEntityModel);
			sevMetadataRepository.removeSEVMetadata(sevEntityModel);
			sevMetadataRepository.createSEVMetadata(sevEntityModel);
		} catch (Exception e) {
			SEVServiceImplLog.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void editSEV(String sevEntityData) throws ServiceException {
		editSEV(parseJsonData(sevEntityData));
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void removeSEV(SEVEntityModel sevEntityModel)
			throws ServiceException {
		try {
			sevRepository.removeSEV(sevEntityModel);
		} catch (Exception e) {
			SEVServiceImplLog.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void removeSEV(String sevEntityData) throws ServiceException {
		removeSEV(parseJsonData(sevEntityData));
	}
}
